package com.aizu.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aizu.utils.SmsUtils;
import com.isp.utils.JdbcUtils;

@Service
public class SmsService extends CommonService {

	private final static String TABLE_SMS = "t_sms";

	/** 是否要发短信 */
	@Value("${sms.send}")
	private String smsSend;
	
	/**
	 * 发送短信
	 * 
	 * @param map
	 * @return
	 */
	public boolean saveSms(String sender, String receiver, String content)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sender", sender);
		map.put("receiver", receiver);
		map.put("content", content);
		map.put("create_date", new Date());

		Map<String, Object> createSqlParam = JdbcUtils.createSqlParam(
				JdbcUtils.SQLTYPE_INSERT, TABLE_SMS, map, null);
		boolean result = this.execute(
				(String) createSqlParam.get(JdbcUtils.SQL),
				(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		if (result && "true".equalsIgnoreCase(smsSend)) {
			// 写表成功并且要发短信，则发送短信
			SmsUtils.sendSms(receiver, content);
		}
		return result;
	}

}
